function e(e) {
    if (e && e.__esModule) return e;
    var t = {};
    if (null != e) for (var i in e) Object.prototype.hasOwnProperty.call(e, i) && (t[i] = e[i]);
    return t.default = e, t;
}

function t(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

var i = t(require("../../public/utils/util")), s = t(require("../../public/utils/user")), n = e(require("../../public/utils/request")), o = e(require("../../public/utils/host"));

Component({
    properties: {
        isShown: {
            value: !1,
            type: Boolean,
            observer: function(e) {
                this.setData({
                    isShown: e
                }), e && this.statisticslogin("authpage");
            }
        }
    },
    data: {
        isShown: !1
    },
    methods: {
        getuserinfo: function(e) {
            var t = this, i = e.detail.errMsg;
            if ("wxwork" === getApp().globalData.environment) wx.qy.getEnterpriseUserInfo({
                success: function(e) {
                    i.includes("qy.getEnterpriseUserInfo:fail") || t.authuser(e);
                }
            }); else if (!i.includes("getUserInfo:fail")) {
                var s = e.detail;
                this.authuser(s);
            }
        },
        authuser: function(e) {
            var t = this, i = getApp().globalData, a = i.environment, r = i.app, u = i.appid, c = i.tokens, l = c.corpid, p = c.userid, d = c.openid, f = e.encryptedData, g = e.iv, h = "wxwork" === a, w = o.apiPrefix + "/user/authuserv2", v = o.apiHost + "/miniappsuite/user/authuser", x = h ? v : w, b = Object.assign({
                app: r,
                appid: u,
                encryptedData: f,
                iv: g
            }, h ? {
                corpid: l,
                userid: p,
                openid: d
            } : {});
            wx.showLoading({
                title: "授权中..."
            }), this.getLoginCodeOrSessionkey().then(function(e) {
                Object.assign(b, e), console.log(b, x, "授权用户参数"), n.post(x, b).then(function(e) {
                    var i = e.code, n = e.data;
                    0 === i ? (s.default.updateTokens(n), wx.showToast({
                        title: "授权成功"
                    }), t.statisticslogin("authuser"), t.reLoad()) : t.badToast(e.message), wx.hideLoading();
                });
            });
        },
        getLoginCodeOrSessionkey: function() {
            var e = this;
            return new Promise(function(t, i) {
                var s = getApp().globalData, n = s.tokens.sessionkey, o = "wxwork" === s.environment, a = o ? wx.qy.checkSession : wx.checkSession, r = o ? wx.qy.login : wx.login;
                a({
                    success: function() {
                        n ? (console.log(n, "checkSession success"), t({
                            sessionkey: n
                        })) : e.badToast("sessionkey为空");
                    },
                    fail: function() {
                        r({
                            success: function(e) {
                                var i = e.code;
                                t({
                                    code: i
                                });
                            },
                            fail: function(t) {
                                e.badToast(t.errMsg);
                            }
                        });
                    }
                });
            });
        },
        reLoad: function() {
            var e = getCurrentPages(), t = e[e.length - 1], s = "/" + t.route + "?" + i.default.serializeParams(t.__query);
            1 === e.length && wx.reLaunch ? wx.reLaunch({
                url: s
            }) : wx.redirectTo({
                url: s
            });
        },
        badToast: function(e) {
            wx.showModal({
                title: "授权失败",
                content: e,
                showCancel: !1,
                confirmText: "知道了"
            });
        },
        colseDialog: function() {
            this.setData({
                isShown: !1
            });
        },
        statisticslogin: function(e) {
            var t = getApp().globalData, i = t.appid, s = t.tokens.openid, a = o.apiPrefix + "/user/statisticslogin";
            return n.post(a, {
                appid: i,
                openid: s,
                op: e
            }).then(function(t) {
                console.log(t, "获取用户信息" + e);
            });
        }
    }
});